<cfinclude template="/ecada/header.cfm">

<cfquery datasource="exp" name="experiments">
    	SELECT *
		FROm givesanswer
		WHERE trialid=#trialid#
		ORDER BY participantid
</cfquery>
    
   
<cfquery datasource="exp" name="experimentscorrect">
    	SELECT participantid, s.answer 
    	FROM givesanswer ga,trial t, stimuli s
    	WHERE trialid=#trialid# 
    	AND ga.trialid=t.id
    	AND s.id=t.stimid
    	AND t.id=#trialid#
    	ORDER BY participantid
</cfquery>

<cfquery datasource="exp" name="correctno">
    	SELECT participantid, count(s.answer) as tot 
    	FROM givesanswer ga, trial t, stimuli s, participant p
    	WHERE trialid = t.id 
    	AND ga.trialid = t.id
    	AND s.id = t.stimid
    	AND t.id = #trialid#
		AND ga.answer = s.answer
		AND ga.participantid = p.id
		GROUP BY s.answer
</cfquery>

<cfquery datasource="exp" name="totno">
    	SELECT count(participantid) as tot 
    	FROM givesanswer ga, trial t, stimuli s 
    	WHERE trialid = t.id
    	AND ga.trialid = t.id
    	AND s.id = t.stimid
    	AND t.id = #trialid#	
</cfquery> 

<cfquery datasource="exp" name="trialinfo">

    	SELECT question, answer, location
		FROM trial t, stimuli s
		WHERE t.id = #trialid#
    	and t.stimid = s.id
		
</cfquery>
   
<cfquery datasource="exp" name="getmax">
	SELECT max(answer) as maxnum
	FROM givesanswer
	WHERE trialid=#trialid#
</cfquery>

<cfquery datasource="exp" name="getmin">
	SELECT min(answer) as minnum
	FROM givesanswer 
	WHERE trialid=#trialid#
</cfquery>



<cfif getmin.minnum neq getmax.maxnum>
	<cfset startval = getmin.minnum>
<cfelse>
	<cfset startval= getmax.maxnum / 2>
</cfif>

<cfset endval = getmax.maxnum>
<cfset diff = endval - startval>
<cfset startval -= diff>
<cfset endval = endval + diff>

<!---START OF DETAIL--->
<cfquery datasource="exp" name="getall">
	SELECT answer 
	FROM givesanswer
	WHERE trialid=#trialid#
</cfquery>

<cfset ans = ArrayNew(1)>

<cfloop query="getall">
	<cfset ArrayAppend(ans, "#answer#")>
</cfloop>

<cfset total = 0>

<cfset number = #ArrayLen(ans)#>

<cfloop index = "i" from = "1" to = "#ArrayLen(ans)#">
	<cfset total += ans[i]>
</cfloop>

<!--- this is to calculate the average, max and min--->
<cfset incorrect = totno.tot - correctno.tot>
<cfset average = (incorrect / totno.tot) * 100>  
<cfset average = round( average * 100 ) / 100>
<cfset mean = total / ArrayLen(ans) >
<cfset mean = round( mean * 100 ) / 100>
<cfset max = ArrayMax(ans) >
<cfset min = ArrayMin(ans) > 

<cfset Temp= ArraySort(ans,"Numeric","desc")>  
  
<!--- this is to calculate the std deviation--->
<cfset s = 0>

<cfloop from="1" to="#ArrayLen(ans)#" index="i">
  <cfset d = ans[i] - mean>
  <cfset s += ( d * d )>
</cfloop>

<cfset st = s / ArrayLen(ans)>
<cfset sd = round( sqr( st ) )>

<!--- this is to calculate the median--->

<cfif ArrayLen(ans) mod 2 is 0>
	<cfset firstmed = ans[ ( ArrayLen(ans) / 2 ) + 1]>
	<cfset secondmed = ans[ ( ArrayLen(ans) / 2 ) - 1]>
	<cfset median = ( firstmed + secondmed ) / 2>
<cfelse>
	<cfset median = ans[ ArrayLen(ans) / 2]>
</cfif>


<!---END OF DETAIL--->

<cfoutput>
	<h1>Answers Given per trial</h1>
	<table>
		<tr>
			<td>
				<img src="/ecada/#trialinfo.location#" width=200>
			</td>
			<td>Question:  #trialinfo.question#<br>
				Correct Answer: #trialinfo.answer#<br>
				Percentage incorrect: #average# <br> 
				Median #median# <br>
				Mean #mean#<br>
				Standard Deviation #sd# <br>
				Max #max# <br>
				Min #min# <br>
			</td>
		</tr>
	</table>

	<cfchart
		format="flash"
		chartheight="400"
		chartwidth="500"
		scalefrom="#startval#"
		scaleto="#endval#"
		seriesplacement="default"
		showborder="yes"
		labelformat="number"
		xaxistitle="Participant Id"
		yaxistitle="Answer"
		tipstyle="mouseOver">

			<cfchartseries
				type="line"
				query="experiments"
				itemcolumn="participantid"
				valuecolumn="answer"
				serieslabel="trial"
				seriescolor="FF00FF"
				markerstyle="circle" />
				
			<cfchartseries 
				type="line"
				query="experimentscorrect"
				itemcolumn="participantid"
				valuecolumn="answer"
				serieslabel="trial"
				seriescolor="00ff00"
				markerstyle="circle" />
	</cfchart>
</cfoutput>

<cfinclude template="/ecada/footer.cfm">
